package cwinsor.com.leaf_raking;

import java.util.Arrays;
import java.util.List;

public final class TeamNumber {

	private String name;

	private TeamNumber(String n) { name = n; } // private constructor
	public static final TeamNumber TEAM_01 = new TeamNumber("TEAM_1");
	public static final TeamNumber TEAM_02 = new TeamNumber("TEAM_2");
	public static final TeamNumber TEAM_03 = new TeamNumber("TEAM_3");
	public static final TeamNumber TEAM_04 = new TeamNumber("TEAM_4");
	public static final TeamNumber TEAM_05 = new TeamNumber("TEAM_5");
	public static final TeamNumber TEAM_06 = new TeamNumber("TEAM_6");
	public static final TeamNumber TEAM_07 = new TeamNumber("TEAM_7");
	public static final TeamNumber TEAM_08 = new TeamNumber("TEAM_8");
	public static final TeamNumber TEAM_09 = new TeamNumber("TEAM_9");
	public static final TeamNumber TEAM_10 = new TeamNumber("TEAM_10");
	public static final TeamNumber TEAM_11 = new TeamNumber("TEAM_11");
	public static final TeamNumber TEAM_12 = new TeamNumber("TEAM_12");
	public static final TeamNumber TEAM_13 = new TeamNumber("TEAM_13");
	public static final TeamNumber TEAM_14 = new TeamNumber("TEAM_14");
	public static final TeamNumber TEAM_15 = new TeamNumber("TEAM_15");
	public static final TeamNumber TEAM_16 = new TeamNumber("TEAM_16");
	public static final TeamNumber TEAM_17 = new TeamNumber("TEAM_17");

	public static final TeamNumber TEAM_UNASSIGNED = new TeamNumber("TEAM_UNASSIGNED");

	public static final List<TeamNumber> TEAMS_PLUS_UNASSIGNED = Arrays.asList(
			TEAM_01,
			TEAM_02,
			TEAM_03,
			TEAM_04,
			TEAM_05,
			TEAM_06,
			TEAM_07,
			TEAM_08,
			TEAM_09,
			TEAM_10,
			TEAM_11,
			TEAM_12,
			TEAM_13,
			TEAM_14,
			TEAM_15,
			TEAM_16,
			TEAM_17,
			TEAM_UNASSIGNED);

	public static final List<TeamNumber> TEAMS_NO_U = Arrays.asList(
			TEAM_01,
			TEAM_02,
			TEAM_03,
			TEAM_04,
			TEAM_05,
			TEAM_06,
			TEAM_07,
			TEAM_08,
			TEAM_09,
			TEAM_10,
			TEAM_11,
			TEAM_12,
			TEAM_13,
			TEAM_14,
			TEAM_15,
			TEAM_16,
			TEAM_17);

	/*
	 * name
	 */
	public final String name() { return name; };

	/*
	 * toString (override)
	 */
	public final String toString() { return name(); };


	public Integer toInteger() {
		if (this == TEAM_UNASSIGNED) { return 0; }
		if (this == TEAM_01) { return 1; }
		if (this == TEAM_02) { return 2; }
		if (this == TEAM_03) { return 3; }
		if (this == TEAM_04) { return 4; }
		if (this == TEAM_05) { return 5; }
		if (this == TEAM_06) { return 6; }
		if (this == TEAM_07) { return 7; }
		if (this == TEAM_08) { return 8; }
		if (this == TEAM_09) { return 9; }
		if (this == TEAM_10) { return 10; }
		if (this == TEAM_11) { return 11; }
		if (this == TEAM_12) { return 12; }
		if (this == TEAM_13) { return 13; }
		if (this == TEAM_14) { return 14; }
		if (this == TEAM_15) { return 15; }
		if (this == TEAM_16) { return 16; }
		if (this == TEAM_17) { return 17; }

		Msg.fatal(dS + ": when performing toInteger in Team - hit the default case");
		return null;
	}

	/*
	 * stringToTeam
	 * converts string to team number
	 */
	public static final TeamNumber stringToTeam(String theString) {
		if (theString.equals("")) { return TEAM_UNASSIGNED; }
		if (theString.equals(" ")) { return TEAM_UNASSIGNED; }
		if (theString.equals("0")) { return TEAM_UNASSIGNED; }
		if (theString.equals("1")) { return TEAM_01; }
		if (theString.equals("2")) { return TEAM_02; }
		if (theString.equals("3")) { return TEAM_03; }
		if (theString.equals("4")) { return TEAM_04; }
		if (theString.equals("5")) { return TEAM_05; }
		if (theString.equals("6")) { return TEAM_06; }
		if (theString.equals("7")) { return TEAM_07; }
		if (theString.equals("8")) { return TEAM_08; }
		if (theString.equals("9")) { return TEAM_09; }
		if (theString.equals("10")) { return TEAM_10; }
		if (theString.equals("11")) { return TEAM_11; }
		if (theString.equals("12")) { return TEAM_12; }
		if (theString.equals("13")) { return TEAM_13; }
		if (theString.equals("14")) { return TEAM_14; }
		if (theString.equals("15")) { return TEAM_15; }
		if (theString.equals("16")) { return TEAM_16; }
		if (theString.equals("17")) { return TEAM_17; }

		Msg.fatal(dS + ": in stringToTeam - cannot convert \""
				+ theString 
				+ "\" to a Team");
		return null;
	}

	public static TeamNumber integerToTeam(Integer theInteger) {
		if (theInteger.equals(0)) { return TEAM_UNASSIGNED; }
		if (theInteger.equals(1)) { return TEAM_01; }
		if (theInteger.equals(2)) { return TEAM_02; }
		if (theInteger.equals(3)) { return TEAM_03; }
		if (theInteger.equals(4)) { return TEAM_04; }
		if (theInteger.equals(5)) { return TEAM_05; }
		if (theInteger.equals(6)) { return TEAM_06; }
		if (theInteger.equals(7)) { return TEAM_07; }
		if (theInteger.equals(8)) { return TEAM_08; }
		if (theInteger.equals(9)) { return TEAM_09; }
		if (theInteger.equals(10)) { return TEAM_10; }
		if (theInteger.equals(11)) { return TEAM_11; }
		if (theInteger.equals(12)) { return TEAM_12; }
		if (theInteger.equals(13)) { return TEAM_13; }
		if (theInteger.equals(14)) { return TEAM_14; }
		if (theInteger.equals(15)) { return TEAM_15; }
		if (theInteger.equals(16)) { return TEAM_16; }
		if (theInteger.equals(17)) { return TEAM_17; }

		Msg.fatal(dS + ": in integerToTeam - cannot convert \""
				+ theInteger 
				+ "\" to a Team");
		return null;
	}




	/*
	 * stringContainsTeam
	 * converts string to team number
	 */


	public static TeamNumber stringContainsTeam(String theString) {
		return stringContainsTeam(theString, true);
	}

	public static final TeamNumber stringContainsTeam(String theString, boolean failIfNotFound) {

		if (theString.contains(TeamNumber.TEAM_10.name())) { return TEAM_10; }
		if (theString.contains(TeamNumber.TEAM_11.name())) { return TEAM_11; }
		if (theString.contains(TeamNumber.TEAM_12.name())) { return TEAM_12; }
		if (theString.contains(TeamNumber.TEAM_13.name())) { return TEAM_13; }
		if (theString.contains(TeamNumber.TEAM_14.name())) { return TEAM_14; }
		if (theString.contains(TeamNumber.TEAM_15.name())) { return TEAM_15; }
		if (theString.contains(TeamNumber.TEAM_16.name())) { return TEAM_16; }
		if (theString.contains(TeamNumber.TEAM_17.name())) { return TEAM_17; }

		if (theString.contains(TeamNumber.TEAM_01.name())) { return TEAM_01; }
		if (theString.contains(TeamNumber.TEAM_02.name())) { return TEAM_02; }
		if (theString.contains(TeamNumber.TEAM_03.name())) { return TEAM_03; }
		if (theString.contains(TeamNumber.TEAM_04.name())) { return TEAM_04; }
		if (theString.contains(TeamNumber.TEAM_05.name())) { return TEAM_05; }
		if (theString.contains(TeamNumber.TEAM_06.name())) { return TEAM_06; }
		if (theString.contains(TeamNumber.TEAM_07.name())) { return TEAM_07; }
		if (theString.contains(TeamNumber.TEAM_08.name())) { return TEAM_08; }
		if (theString.contains(TeamNumber.TEAM_09.name())) { return TEAM_09; }
		
		if (failIfNotFound) {
			Msg.fatal(dS + ": in stringContainsTeam - cannot convert "
					+ theString 
					+ " to a Team");
			return null;
		}
		return TEAM_UNASSIGNED;
	}


	/*
	 * update
	 * returns a team, given a teamChange
	 * if the teamChange is any of the ADDs - return that team
	 * if the teamChange is DROP - return TEAM_UNASSIGNED
	 * it the teamChange is NO_CHANGE - return the current team
	 */
	public final TeamNumber updateTeam(TeamChange teamChange) {

		if (teamChange == TeamChange.DROP) { return TeamNumber.TEAM_UNASSIGNED; }
		if (teamChange == TeamChange.NO_CHANGE) { return this; }
		if (teamChange == TeamChange.ADD01) { return TeamNumber.TEAM_01; }
		if (teamChange == TeamChange.ADD02) { return TeamNumber.TEAM_02; }
		if (teamChange == TeamChange.ADD03) { return TeamNumber.TEAM_03; }
		if (teamChange == TeamChange.ADD04) { return TeamNumber.TEAM_04; }
		if (teamChange == TeamChange.ADD05) { return TeamNumber.TEAM_05; }
		if (teamChange == TeamChange.ADD06) { return TeamNumber.TEAM_06; }
		if (teamChange == TeamChange.ADD07) { return TeamNumber.TEAM_07; }
		if (teamChange == TeamChange.ADD08) { return TeamNumber.TEAM_08; }
		if (teamChange == TeamChange.ADD09) { return TeamNumber.TEAM_09; }
		if (teamChange == TeamChange.ADD10) { return TeamNumber.TEAM_10; }
		if (teamChange == TeamChange.ADD11) { return TeamNumber.TEAM_11; }
		if (teamChange == TeamChange.ADD12) { return TeamNumber.TEAM_12; }
		if (teamChange == TeamChange.ADD13) { return TeamNumber.TEAM_13; }
		if (teamChange == TeamChange.ADD14) { return TeamNumber.TEAM_14; }
		if (teamChange == TeamChange.ADD15) { return TeamNumber.TEAM_15; }
		if (teamChange == TeamChange.ADD16) { return TeamNumber.TEAM_16; }
		if (teamChange == TeamChange.ADD17) { return TeamNumber.TEAM_17; }

		Msg.fatal(dS + ": when updating team - hit the default case - team " + name() + " with change " + teamChange.name());
		return null;
	}

	/*
	 * return a single-character representation
	 * with " " for the unassigned case
	 */
	public String toStringSingleCharWithBlank() {
		// if team is unassigned return a blank
		if (this.equals(TEAM_UNASSIGNED)) { return " "; }
		return toInteger().toString();
	}


	// debug
	private static String dS = "Team";
	private static Integer dL = new Integer(0);
}
